package com.wedata.weapp;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;

import java.util.*;

public class 芒果TV {

    public static void main(String[] args) throws Exception {
        Map<String, Object> body = new TreeMap<>();
        body.put("channelId","fc01");
        body.put("uuid","1");
        body.put("userId","2");
        body.put("vipStatus", 2);
        body.put("orderId","3720501919330206976");

        body.put("payTime","20240619110200");
        body.put("beginTime","20240619110200");
        body.put("endTime","20240619110200");

        body.put("timestamp", System.currentTimeMillis());

        StringBuilder builder = new StringBuilder();
        Iterator<String> iterator = body.keySet().iterator();
        while (iterator.hasNext()){
            if(builder.length()>0){
                builder.append("&");
            }
            String key = iterator.next();
            builder.append(key).append("=").append(body.get(key));
        }
        String sign = genSign(builder.toString());
        builder.append("&sign=").append(sign);

        JSONObject jsonObject = HttpUtils.sendPostByForm("http://openott.api.mgtv.com/v1/factory_client/partner/updateuser", builder.toString());
        System.out.println(jsonObject);
    }

    /**
     * 签名算法
     * @param body
     * @return
     */
    private static String genSign(Map<String, Object> body) throws Exception {
        String secretKey = "b84cfb05c0ae60123cd46b003fe6e938";
        StringBuffer signData = new StringBuffer();
        Set<String> set = body.keySet();
        List<String> list = new ArrayList();
        for(String key : set){
            list.add(key);
        }
        Collections.sort(list);
        for(String key : list){
            Object val = body.get(key);
            if(val == null){
                continue;
            }
            signData.append(key).append("=").append(val);
            signData.append("&");
        }
        signData.append("secret_key=").append(secretKey);

        System.out.println(signData.toString());
        //signData = new StringBuffer("auditstate=1&avatar=abc&chid=abc&delay=1&nickname=昵称&openid=123456&suid=123456789&timestamp=1687165771992&secret_key=test123456789");
        byte[] bytes = DigestUtil.sha1(signData.toString().toLowerCase());

        StringBuffer hexString = new StringBuffer();
        for(byte b: bytes){
            String hex = Integer.toHexString(0xff&b);
            if(hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        System.out.println(hexString);

        /*MessageDigest digest = MessageDigest.getInstance("sha1");
        byte[] bytes = digest.digest(signData.toString().toLowerCase().getBytes());

        StringBuffer hexString = new StringBuffer();
        for(byte b: bytes){
            String hex = Integer.toHexString(0xff&b);
            if(hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        System.out.println(hexString);*/

        return hexString.toString();
    }

    /**
     * 签名算法
     * @param data
     * @return
     */
    private static String genSign(String data) {
        String secretKey = "738c7ea0a6774375b9defb7bddd2bc19";
        StringBuilder signData = new StringBuilder(data);
        signData.append("&secret_key=").append(secretKey);

        byte[] bytes = DigestUtil.sha1(signData.toString().toLowerCase());
        StringBuffer hexString = new StringBuffer();
        for(byte b: bytes){
            String hex = Integer.toHexString(0xff&b);
            if(hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }

}
